#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N=110;
const int INF=0X3f3f3f3f;
struct node{
	int to,w;
	bool operator < (const node& p2) const 
	{
		if(w==p2.w)
			return to>p2.to;
		return w>p2.w;	
	}
};
int n,m;
int ret;
int vis[N];
vector<node> edge[N];
int dp[N];
bool dijkstra(int x)
{
	
	for(int i=1;i<=n;i++)
		dp[i]=INF;
	dp[x]=0;
	priority_queue<node,vector<node>> q;
	q.push((node){x,0});
	while(q.size())
	{
		node tmp=q.top();
		vis[tmp.to]=1;
		q.pop();
		for(auto& e:edge[tmp.to])
		{
			if(vis[e.to])
				continue;
			
			if(dp[e.to]>dp[tmp.to]+e.w)
			{
				
				dp[e.to]=dp[tmp.to]+e.w;
				q.push((node){e.to,dp[e.to]});
			}
		}
	}
	for(int i=1;i<=n;i++)
	{	
		if(dp[i]==INF)
			return false;
		ret=max(ret,dp[i]);
	}
	return true;
}

int main()
{
	cin>>n>>m;
	for(int i=0;i<m;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
	
		edge[a].push_back((node){b,c});
		edge[b].push_back((node){a,c});
	}
	if(dijkstra(1))
	{
		cout<<ret;
	}
	else{
		cout<<-1;
	}
	
	
	return 0;
}
